package edu.ncst.baseweb.service.impl.webmanage;

import cn.wintersun.basecommon.common.Result;
import edu.ncst.baseweb.dao.web.LabInfo;
import edu.ncst.baseweb.mapper.LabInfoMapper;
import edu.ncst.baseweb.service.intrer.webmanage.LabInfoManageService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class LabInfoManageImpl implements LabInfoManageService {

    @Autowired private LabInfoMapper LabInfoMapper;


    @Override
    public Result<IPage<LabInfo>> list(LabInfo LabInfo) {
        QueryWrapper<LabInfo> queryWrapper = Wrappers.query();


        Page<LabInfo> LabInfoPage = new Page<>(LabInfo.getCurrent(), LabInfo.getPageSize());
        IPage<LabInfo> LabInfoIPage = LabInfoMapper.selectPage(LabInfoPage, queryWrapper);

        return Result.ok(LabInfoIPage);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<Object> save(LabInfo LabInfo) {
        if (LabInfo.getId() != null) {
            //修改
            LabInfoMapper.updateById(LabInfo);

        } else {
            //新增
            LabInfoMapper.insert(LabInfo);
        }

        return Result.ok();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<Object> delete(String LabInfoId) throws Exception {

        if (StringUtils.isBlank(LabInfoId)) {
            throw new Exception("被删除的id为空");
        }

        List<String> ids = Arrays.stream(LabInfoId.split(",")).collect(Collectors.toList());

        // 删除  id 在 ids 集合中的记录
        LabInfoMapper.deleteBatchIds(ids);

        return Result.failure(200, "删除成功！");
    }
}
